1 00:00:00,540 --> 00:00:02,970 In this video, we're going to learn about functions. 2 00:00:02,970 --> 00:00:05,940 And a function is a block of code that you can call. 3 00:00:05,970 --> 00:00:10,710 Functions are great for handling repetitive tasks and also for handling events. 4 00:00:10,710 --> 00:00:12,870 Like I add this button right here. 5 00:00:12,870 --> 00:00:20,820 If I press it, it's going to fire an event that calls a function to create steps, and each step uses 6 00:00:20,820 --> 00:00:22,940 a function to create the step itself. 7 00:00:22,950 --> 00:00:25,830 I thought that would be a pretty cool way to learn about functions. 8 00:00:25,830 --> 00:00:27,750 Let's go ahead and get started with that. 9 00:00:29,040 --> 00:00:29,310 All right. 10 00:00:29,310 --> 00:00:33,960 So I think I'm going to use a base plate today, click that just to change things up. 11 00:00:34,590 --> 00:00:39,810 And the big difference with the baseplate, especially for us today, is the Y axis. 12 00:00:39,810 --> 00:00:45,120 The height of the baseplate starts at zero on the terrain, the Y starts at ten. 13 00:00:45,150 --> 00:00:48,690 It used to be 2.5, and then they increase the thickness of the terrain. 14 00:00:48,690 --> 00:00:50,260 This is going to be zero. 15 00:00:50,280 --> 00:00:51,720 Just keep that in mind. 16 00:00:51,900 --> 00:00:52,290 All right. 17 00:00:52,320 --> 00:00:54,690 So let's go ahead and get started with our steps. 18 00:00:54,690 --> 00:01:02,520 Let's go to this workspace, open the workspace up, hit that plus sign, add a script, or we can just 19 00:01:02,520 --> 00:01:06,480 type out as R to see the list of things that begin with SCR. 20 00:01:06,690 --> 00:01:07,320 There we go. 21 00:01:07,320 --> 00:01:08,220 We've got a script. 22 00:01:08,220 --> 00:01:10,470 Let's call the script steps. 23 00:01:12,360 --> 00:01:13,050 All right. 24 00:01:13,050 --> 00:01:18,750 Now, before we had a part in the workspace in the patio video and then we just modified it. 25 00:01:18,750 --> 00:01:22,170 But here what I'm going to do is I'm going to create a part from scratch. 26 00:01:22,170 --> 00:01:24,420 So I'll do my local part variable. 27 00:01:24,420 --> 00:01:30,420 But instead of equals script parent, we're going to have equals instance dot new. 28 00:01:30,420 --> 00:01:37,530 So we are creating a new instance of parentheses, speech, quote, part. 29 00:01:37,650 --> 00:01:40,260 We can create a lot of cool stuff and we will. 30 00:01:40,260 --> 00:01:42,300 But right now we're focusing on parts. 31 00:01:42,540 --> 00:01:46,800 The second argument is going to be the parent of the part. 32 00:01:46,800 --> 00:01:52,080 You have to have it in the workspace or a descendant of something in the workspace in order for it to 33 00:01:52,080 --> 00:01:53,010 show up in your game. 34 00:01:53,010 --> 00:01:55,110 So we'll say workspace. 35 00:01:55,620 --> 00:01:56,250 Cool. 36 00:01:56,250 --> 00:02:03,030 We also need to set the position, the default position of a part when you create it is going to be 37 00:02:03,030 --> 00:02:04,260 000. 38 00:02:04,260 --> 00:02:06,240 Well, that's where our base plate is. 39 00:02:06,240 --> 00:02:09,090 And then terrain, it's actually ten studs above that. 40 00:02:09,090 --> 00:02:11,070 So you won't even see it in the terrain. 41 00:02:11,220 --> 00:02:14,850 Let's go ahead and create the part or create the position. 42 00:02:14,940 --> 00:02:15,930 Create the position. 43 00:02:15,930 --> 00:02:25,350 Vector should say we'll do new zero on the x that center of the world on the x axis to studs. 44 00:02:25,350 --> 00:02:26,010 Hi. 45 00:02:26,010 --> 00:02:30,150 We can do that because the base plate starts at zero and then we'll move it. 46 00:02:30,150 --> 00:02:32,460 Three studs on the Z axis. 47 00:02:33,060 --> 00:02:33,900 Cool. 48 00:02:33,930 --> 00:02:35,850 Let's change the size of it. 49 00:02:36,360 --> 00:02:39,060 That's also a vector three, right? 50 00:02:39,060 --> 00:02:43,830 So the default size, if you create a part, will be four one and two. 51 00:02:43,920 --> 00:02:45,600 Let's make this look like a step. 52 00:02:45,600 --> 00:02:53,580 So we're going to say ten studs on the X is going to be kind of a long step, 0.5 studs high, not too 53 00:02:53,580 --> 00:02:57,740 thick, and then we'll make this three studs deep. 54 00:02:58,440 --> 00:02:59,130 Cool. 55 00:02:59,130 --> 00:03:02,970 But because the position is a little off the ground, we don't want it to fall. 56 00:03:02,970 --> 00:03:04,020 Let's anchor it. 57 00:03:04,020 --> 00:03:07,140 So we'll say part anchored equals true. 58 00:03:07,830 --> 00:03:08,220 All right. 59 00:03:08,220 --> 00:03:08,660 Let's play it. 60 00:03:08,670 --> 00:03:09,480 See what we got. 61 00:03:12,790 --> 00:03:13,730 There we go. 62 00:03:13,750 --> 00:03:16,300 It's floating above our spa location. 63 00:03:16,330 --> 00:03:18,530 Notice these little studs right here. 64 00:03:18,550 --> 00:03:20,680 That's where the term stud comes from. 65 00:03:20,680 --> 00:03:22,700 The distance between these two things. 66 00:03:22,720 --> 00:03:23,940 It's from the Lego days. 67 00:03:23,950 --> 00:03:26,530 And if you look on the bottom, I don't know if you could see it. 68 00:03:26,560 --> 00:03:27,500 You could see. 69 00:03:27,500 --> 00:03:28,230 Yeah, there we go. 70 00:03:28,240 --> 00:03:29,650 You could see little studs underneath. 71 00:03:29,650 --> 00:03:31,330 Those are the little inlets of the studs. 72 00:03:31,330 --> 00:03:31,780 Right. 73 00:03:32,410 --> 00:03:32,830 All right. 74 00:03:32,830 --> 00:03:33,610 So let's get rid of that. 75 00:03:33,610 --> 00:03:36,670 Let's make that smooth pit stop. 76 00:03:38,480 --> 00:03:39,570 And we'll do part. 77 00:03:39,590 --> 00:03:45,050 Now we have something called surface type that begins with an S surface. 78 00:03:45,050 --> 00:03:45,780 Top surface. 79 00:03:45,800 --> 00:03:46,280 I'm sorry. 80 00:03:46,520 --> 00:03:48,860 Top surface, which is a surface type. 81 00:03:48,920 --> 00:03:49,610 There we go. 82 00:03:49,880 --> 00:03:51,890 Top surface enum. 83 00:03:51,890 --> 00:03:53,420 We learned about that for. 84 00:03:53,420 --> 00:03:56,690 And we got to get a surface type here. 85 00:03:56,720 --> 00:03:57,290 There we go. 86 00:03:57,290 --> 00:03:58,220 Surface type. 87 00:03:58,220 --> 00:03:59,390 Smooth. 88 00:03:59,750 --> 00:04:00,170 Cool. 89 00:04:00,170 --> 00:04:03,920 So this enum just holds a whole bunch of stuff that you might need in road roadblocks. 90 00:04:03,920 --> 00:04:05,810 And one of those things is surface type. 91 00:04:05,810 --> 00:04:09,710 Surface type has a whole bunch of different surface types and we've got smooth. 92 00:04:09,860 --> 00:04:20,530 We're going to do that for the bottom two bottom surface equals enum air, enum surface type smooth. 93 00:04:20,540 --> 00:04:22,490 Now we've got a smooth step. 94 00:04:22,640 --> 00:04:23,630 Let's try it. 95 00:04:27,280 --> 00:04:28,810 All right, there's our step now. 96 00:04:28,810 --> 00:04:30,220 We want to make a couple of steps. 97 00:04:30,220 --> 00:04:31,450 How do we do that? 98 00:04:31,600 --> 00:04:33,780 Well, let's pretend we don't know about functions. 99 00:04:33,790 --> 00:04:35,290 Most of you guys already do. 100 00:04:35,830 --> 00:04:39,880 Let's copy this code control C and we'll make three steps. 101 00:04:40,480 --> 00:04:45,970 Control V, control V, we've got three steps here. 102 00:04:45,970 --> 00:04:46,420 Right? 103 00:04:46,420 --> 00:04:48,250 But they're going to be exactly in the same position. 104 00:04:48,250 --> 00:04:49,690 We have to change the position. 105 00:04:49,690 --> 00:04:51,580 So this is two and three. 106 00:04:51,910 --> 00:04:55,510 Let's make this position here for the second step. 107 00:04:55,600 --> 00:05:00,880 Come up by four and then move it down the Z another three. 108 00:05:00,880 --> 00:05:02,740 So we'll make that six. 109 00:05:03,190 --> 00:05:05,140 We're going to do the same thing on this step. 110 00:05:05,500 --> 00:05:07,510 It was two then for now. 111 00:05:07,510 --> 00:05:09,070 We'll make that six. 112 00:05:09,760 --> 00:05:10,990 We'll make this nine. 113 00:05:11,410 --> 00:05:12,010 There we go. 114 00:05:12,010 --> 00:05:13,360 Let's give it a shot. 115 00:05:17,640 --> 00:05:18,090 There. 116 00:05:18,090 --> 00:05:19,710 We got three steps. 117 00:05:19,770 --> 00:05:24,840 Let's take a look at this code, because if you are a Java programmer, you'd be like, Hey, we just 118 00:05:24,840 --> 00:05:27,130 re declared this several times. 119 00:05:27,150 --> 00:05:28,500 How come we don't have an error? 120 00:05:28,590 --> 00:05:30,210 Well, I'll let you do that. 121 00:05:30,210 --> 00:05:34,290 But if you feel better, what we can do is get rid of that local. 122 00:05:34,350 --> 00:05:36,240 Makes it look like a declaration. 123 00:05:37,050 --> 00:05:38,870 And now we have the part. 124 00:05:38,880 --> 00:05:41,120 We're just re assigning another value. 125 00:05:41,130 --> 00:05:46,290 So it is three parts, but we only have a reference to the latest one. 126 00:05:46,290 --> 00:05:46,710 Right. 127 00:05:46,710 --> 00:05:51,930 So this part right here is going to be pointing to this instance of the part. 128 00:05:51,930 --> 00:05:56,250 So all the changes that we make here are right are this part right here. 129 00:05:56,250 --> 00:06:06,840 So if I do something like part brick, color equals brick color dot red parentheses, only the last 130 00:06:06,840 --> 00:06:07,470 one will be red. 131 00:06:07,470 --> 00:06:08,760 It's not going to be all of these. 132 00:06:08,760 --> 00:06:12,870 Right, because we get a new address every time we're pointing to a new reference of the part. 133 00:06:14,600 --> 00:06:15,120 There you go. 134 00:06:15,140 --> 00:06:16,610 See, just the last one. 135 00:06:17,540 --> 00:06:18,190 All right. 136 00:06:18,200 --> 00:06:21,050 Now, this is going to get really, really repetitive, right? 137 00:06:21,050 --> 00:06:23,060 Especially if we have to change something like the size. 138 00:06:23,070 --> 00:06:24,980 You're going to have to go through every single line. 139 00:06:24,980 --> 00:06:30,380 But everything is so close to the same thing except possession. 140 00:06:31,100 --> 00:06:35,630 While we could do a function and then just pass in the position that we want. 141 00:06:36,080 --> 00:06:41,000 So I'm going to say a local function make step. 142 00:06:41,330 --> 00:06:41,910 Right. 143 00:06:42,300 --> 00:06:44,210 We'll get this code right here. 144 00:06:44,240 --> 00:06:46,730 Control X to cut. 145 00:06:46,880 --> 00:06:49,310 Control V to paste. 146 00:06:49,880 --> 00:06:51,980 I get rid of these two. 147 00:06:53,300 --> 00:06:55,970 Let's keep our brick color, because I want to look at this real quick. 148 00:06:56,000 --> 00:06:59,840 See how that complained unknown global part. 149 00:06:59,930 --> 00:07:06,760 It doesn't know what this thing is because we declared it inside this code block. 150 00:07:06,770 --> 00:07:11,180 We talked about scope before, but it was up here on the top. 151 00:07:11,180 --> 00:07:13,040 We had the local part. 152 00:07:13,040 --> 00:07:17,590 Well, that's local to the script, just like this is local to the script. 153 00:07:17,600 --> 00:07:24,440 Once we put it inside a function that's a smaller code block, we can't see it outside this function. 154 00:07:24,440 --> 00:07:26,300 It's local to that function. 155 00:07:26,990 --> 00:07:27,410 All right. 156 00:07:27,410 --> 00:07:28,570 Well, that's okay. 157 00:07:28,580 --> 00:07:32,360 I will show you how to get around stuff like that for now. 158 00:07:32,660 --> 00:07:37,160 How about we change the color by doing this? 159 00:07:37,250 --> 00:07:42,320 That way, we'll change every step color and we'll make it random. 160 00:07:43,070 --> 00:07:43,950 Cool beans. 161 00:07:43,970 --> 00:07:45,140 Now, will this run? 162 00:07:45,140 --> 00:07:45,800 No. 163 00:07:45,800 --> 00:07:48,170 We have to call, make step. 164 00:07:48,170 --> 00:07:49,730 We have to call it somehow. 165 00:07:50,150 --> 00:07:51,460 Well, it's easy. 166 00:07:51,470 --> 00:07:52,670 Let's copy that. 167 00:07:54,470 --> 00:07:54,780 Boom. 168 00:07:54,920 --> 00:07:56,040 And then we paste it. 169 00:07:56,060 --> 00:08:00,410 Now, what's going to happen is the interpreter is going to start this script up in the game or start 170 00:08:00,410 --> 00:08:01,080 reading the script. 171 00:08:01,080 --> 00:08:02,330 When the game starts up. 172 00:08:02,360 --> 00:08:05,090 It's going to read this in the memory, it's going to know it exists. 173 00:08:05,090 --> 00:08:07,810 And then when we call it, it's going to fire it. 174 00:08:07,820 --> 00:08:10,610 If this was not here, this would not run. 175 00:08:10,880 --> 00:08:15,170 The interpreter would know about make step, but it wouldn't be running it now. 176 00:08:15,170 --> 00:08:15,700 It does. 177 00:08:15,710 --> 00:08:17,330 We only have one step though. 178 00:08:18,470 --> 00:08:19,010 Bummer. 179 00:08:19,010 --> 00:08:20,900 We need to make many steps. 180 00:08:22,140 --> 00:08:25,680 Well, this is the only thing that needs to change. 181 00:08:25,710 --> 00:08:27,270 Wouldn't it be great? 182 00:08:27,480 --> 00:08:31,460 Notice this vector three new as an X, Y and z. 183 00:08:31,470 --> 00:08:34,050 We need to change the Y and the Z. 184 00:08:34,560 --> 00:08:38,210 Let's just call this a Y and a Z. 185 00:08:38,220 --> 00:08:39,720 These are going to be variables. 186 00:08:39,720 --> 00:08:43,920 It's complaining now that you see that red line unknown global z. 187 00:08:44,160 --> 00:08:47,940 Well, we can declare those variables up here. 188 00:08:49,200 --> 00:08:53,100 These are called parameters in the function definition. 189 00:08:53,100 --> 00:08:56,910 They're going to be local variables to this function. 190 00:08:57,420 --> 00:09:05,280 If we go down here and we pass the numbers two and three, this two is going to be assigned to that 191 00:09:05,310 --> 00:09:06,120 Y. 192 00:09:06,120 --> 00:09:08,760 And then when we use that Y, it's going to be a two. 193 00:09:08,790 --> 00:09:11,010 Same with three for Z. 194 00:09:11,220 --> 00:09:14,850 Oh, that's a three now and then that's a three. 195 00:09:15,420 --> 00:09:15,960 Why is that? 196 00:09:15,960 --> 00:09:16,450 Great. 197 00:09:16,470 --> 00:09:18,870 Well, we can do this copy. 198 00:09:20,320 --> 00:09:21,970 Paste, paste, paste. 199 00:09:21,970 --> 00:09:23,200 Let's make four steps. 200 00:09:23,200 --> 00:09:25,330 So these are incrementing by two. 201 00:09:25,330 --> 00:09:30,640 So we'll say two, four, six, eight. 202 00:09:30,670 --> 00:09:32,560 These are increasing by three. 203 00:09:32,560 --> 00:09:38,080 So it's three, six, nine, 12. 204 00:09:38,380 --> 00:09:39,610 Let's see what we got. 205 00:09:43,770 --> 00:09:44,780 Oh, look at that. 206 00:09:44,790 --> 00:09:46,380 The random numbers aren't that good. 207 00:09:46,380 --> 00:09:49,650 But don't worry, when we do tables, we're going to change that stuff. 208 00:09:50,670 --> 00:09:51,360 All right. 209 00:09:51,370 --> 00:09:53,130 One more thing. 210 00:09:53,550 --> 00:09:56,040 We haven't done any return statements and stuff like that. 211 00:09:56,040 --> 00:09:57,390 That'll be the next video. 212 00:09:57,420 --> 00:09:59,670 I'm going to move that up, get rid of that whitespace. 213 00:09:59,670 --> 00:10:01,890 I'm just going to put a little pause in here. 214 00:10:02,400 --> 00:10:05,820 In between each step, I'm going to wait 0.2 seconds. 215 00:10:05,820 --> 00:10:09,900 And then before I start making any steps, I want to see them be made. 216 00:10:09,900 --> 00:10:17,280 So I'm going to say, wait, I'll do 7 seconds, let's do 6 seconds, and then watch them be made. 217 00:10:17,880 --> 00:10:18,750 Hey, we go. 218 00:10:22,740 --> 00:10:23,880 And any second. 219 00:10:26,180 --> 00:10:29,510 There they go, the four steps and we have different colors. 220 00:10:29,630 --> 00:10:30,190 All right. 221 00:10:30,200 --> 00:10:33,890 Next video we're going to do return statement and we're going to do some other stuff.